package com.ks.api.utils;

import com.ks.api.entity.KsConfigSc;
import com.ks.api.mapper.KsConfigScMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
 * 配置数据迁移工具
 * 用于将旧的配置文件中的配置迁移到数据库中
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class DatabaseMigrationUtil implements CommandLineRunner {

    private final KsConfigScMapper KsConfigScMapper;
    
    @Value("${kuaishou.miniapp.getTokenappid}")
    private String appId;
    
    @Value("${kuaishou.miniapp.kaifangpingtai_sc}")
    private String appSecret;
    
    @Value("${kuaishou.miniapp.auth_code}")
    private String authCode;
    
    @Value("${kuaishou.miniapp.advertiser_id}")
    private String advertiserId;
    
    @Value("${kuaishou.miniapp.appid}")
    private String ztId;
    
    @Value("${kuaishou.miniapp.secret}")
    private String ztSc;
    
    @Value("${kuaishou.miniapp.zy_getTokenappid}")
    private String zyAppId;
    
    @Value("${kuaishou.miniapp.zy_kaifangpingtai_sc}")
    private String zyAppSecret;
    
    @Value("${kuaishou.miniapp.zy_auth_code}")
    private String zyAuthCode;
    
    @Value("${kuaishou.miniapp.zy_advertiser_id}")
    private String zyAdvertiserId;
    
    @Value("${kuaishou.miniapp.zy_appid}")
    private String zyZtId;
    
    @Value("${kuaishou.miniapp.zy_secret}")
    private String zyZtSc;

    @Override
    public void run(String... args) throws Exception {
        migrateConfigIfNeeded();
    }
    
    /**
     * 如果数据库中没有配置信息，从配置文件迁移
     */
    private void migrateConfigIfNeeded() {
        try {
            // 检查是否已有配置
            if (KsConfigScMapper.selectCount(null) == 0) {
                log.info("检测到数据库中没有应用配置，开始从配置文件迁移...");
                
                // 迁移第一个应用的配置
                KsConfigSc app1Config = new KsConfigSc();
                app1Config.setKfId(appId);
                app1Config.setKfSc(appSecret);
                app1Config.setAuthCode(authCode);
                app1Config.setAdvertiserId(advertiserId);
                app1Config.setZtId(ztId);
                app1Config.setZtSc(ztSc);
                app1Config.setAppName("游戏");
                app1Config.setAppDh("app1");
                
                // 迁移第二个应用的配置
                KsConfigSc app2Config = new KsConfigSc();
                app2Config.setKfId(zyAppId);
                app2Config.setKfSc(zyAppSecret);
                app2Config.setAuthCode(zyAuthCode);
                app2Config.setAdvertiserId(zyAdvertiserId);
                app2Config.setZtId(zyZtId);
                app2Config.setZtSc(zyZtSc);
                app2Config.setAppName("智月小游戏");
                app2Config.setAppDh("app2");
                
                // 保存到数据库
                KsConfigScMapper.insert(app1Config);
                KsConfigScMapper.insert(app2Config);
                
                log.info("配置迁移完成，共迁移 2 个应用配置");
            } else {
                log.info("数据库中已有应用配置，无需迁移");
            }
        } catch (Exception e) {
            log.error("配置迁移失败", e);
        }
    }
} 